package org.openotdr.server.file.xml;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;

public abstract class TableParser {
	public void parse(Document document, Element root) {
		Element tableRoot = document.createElement("Table");
		root.appendChild(tableRoot);

		Element tableCount = document.createElement("TableCount");
		tableRoot.appendChild(tableCount);
		Text countVal = document.createTextNode(getTableCount().toString());
		tableCount.appendChild(countVal);
		
		for (Integer iTable = 0; iTable < getTableCount(); iTable++) {
			Element table = document.createElement("TableElement");
			tableRoot.appendChild(table);
			
			table.setAttribute("id", iTable.toString());
			table.setAttribute("name", getName(iTable));
			
			Element hiddenCount = document.createElement("HiddenColumnCount");
			table.appendChild(hiddenCount);
			Text hiddenVal = document.createTextNode(getHiddenColumnCount().toString());
			table.appendChild(hiddenVal);
			
			for (Integer iHidden = 0 ; iHidden < getHiddenColumnCount(); iHidden++) {
				Element hiddenColumn = document.createElement("HiddenColumn");
				table.appendChild(hiddenColumn);
				Text hiddenColumnVal = document.createTextNode(getHiddenColumn(iTable, iHidden).toString());
				hiddenColumn.appendChild(hiddenColumnVal);
				
			}
		}
	}

	abstract protected Integer getHiddenColumn(Integer iTable, Integer iHidden);
	abstract protected Integer getHiddenColumnCount();
	abstract protected String getName(Integer iTable);
	abstract protected Integer getTableCount();
}
